﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Media;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace WhoWants
{
    public partial class frmMain : Form
    {
        string imagedir = "d:\\resources";
        public frmMain()
        {
            InitializeComponent();
        }
        XElement xQuestions=new XElement("Questions");
        IEnumerable<XElement> xquestion;
        int totalQuestions = 0;
        int currentQuestion = 0;
        int currectAnswer = 0;
        string successMessage = string.Empty;
        string failMessage = string.Empty;
        bool bloadQuestion = false;
        private void Form1_Load(object sender, EventArgs e)
        {
            var qFile= Settings1.Default.ResourceDirectory+"\\Questions.xml";
            xQuestions = XElement.Load(qFile);
            currentQuestion = 0;
            xquestion = from question in xQuestions.Elements("Question")
                            select question;
            totalQuestions = xquestion.Count();
           
            loadQuestion();
           

        }

        private void loadQuestion()
        {
            try
            {
                var curQuesion = xquestion.ElementAt(currentQuestion);
                labelQuestionNo.Text = string.Format("Question {0}/{1}", currentQuestion + 1, totalQuestions);
                labelQustion.Text = curQuesion.Attribute("question").Value;
                if (!string.IsNullOrEmpty(curQuesion.Attribute("photo").Value))
                {
                    var photo = Settings1.Default.ResourceDirectory + string.Format("\\{0}", curQuesion.Attribute("photo").Value);

                    pictureBoxPhoto.Load(photo);
                    pictureBoxPhoto.Visible = true;
                }
                else
                    pictureBoxPhoto.Visible = false;
                successMessage = curQuesion.Attribute("success").Value;
                failMessage = curQuesion.Attribute("fail").Value;
                var xanswer = from question in curQuesion.Elements("answers").Elements("answer")
                              select question;
                for (int i = 0; i < 4; i++)
                {
                    var controlName = string.Format("checkBox{0}", i + 1);
                    groupBox1.Controls[controlName].Text = (char)(65 + i) + ". " + xanswer.ElementAt(i).Value;
                    groupBox1.Controls[controlName].Visible = true;
                    groupBox1.Controls[controlName].ForeColor = Color.Black;
                    if (xanswer.ElementAt(i).Attributes().Count() > 0)
                        currectAnswer = i;
                }
                foreach (CheckBox chkbox in groupBox1.Controls)
                {

                    chkbox.Checked = false;


                }
            }
            catch
            {
                currentQuestion = 0;
            }

        }

      

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            foreach (CheckBox chkbox in groupBox1.Controls)
            {
                if (chkbox.Name != (sender as CheckBox).Name)
                {
                    chkbox.Checked = false;
                }

            }
        }

        private void checkBox1_CheckedChanged_1(object sender, EventArgs e)
        {

        }
        private void showResult(string htmlSource)
        {
            if (this.webBrowser1.Document == null)
            {
                this.webBrowser1.DocumentText = htmlSource;
            }
            else
            {
                this.webBrowser1.Document.OpenNew(true);
                this.webBrowser1.Document.Write(htmlSource);
            }
        }

        private void buttonNext_Click(object sender, EventArgs e)
        {
            bloadQuestion = false;
            var selectedQuestion = -1;
            foreach (CheckBox chkbox in groupBox1.Controls)
            {
                if (chkbox.Checked)
                    selectedQuestion = Convert.ToInt32(chkbox.Name.Substring(8));
               

            }
            if (selectedQuestion != -1)
            {
                
                    
                    if (correctAnswer(selectedQuestion))
                    {



                            SoundPlayer simpleSound = new SoundPlayer(Settings1.Default.Soundfiles + "\\applause3.wav");
                            simpleSound.Play();
                            labelResult.Text = "Correct";
                            labelResult.ForeColor = Color.Green;

                            showResult( string.Format("<img src='{1}\\congrats.jpg'/><br/><h1  style='color:black'>{0}</h1>", successMessage, Settings1.Default.Soundfiles));
                            
                            currentQuestion++;
                            if (currentQuestion >= totalQuestions)
                            {
                                
                                
                            }
                            else
                            {
                                bloadQuestion = true;
                            }
                        
                    }
                    else
                    {
                        labelResult.Text = "Incorrect";
                        labelResult.ForeColor = Color.Red;
                        
                        
                        showResult( string.Format("<img src='{1}\\sorry.jpg'/><br/><h1  style='color:black'>{0}</h1>", failMessage,Settings1.Default.Soundfiles));
                        
                        SoundPlayer simpleSound = new SoundPlayer(Settings1.Default.Soundfiles + "\\explosion_x.wav");
                        simpleSound.Play();
                    }
                    groupBox2.Visible = true;
               
            }
            else
            {  

            }
        }

        private bool correctAnswer(int selectedAnswer)
        {
            if (currectAnswer + 1 == selectedAnswer)
            {
                var controlName = string.Format("checkBox{0}", selectedAnswer);
                groupBox1.Controls[controlName].ForeColor = Color.Green;


                return true;
            }
            else
            {
                var controlName = string.Format("checkBox{0}", selectedAnswer);
                groupBox1.Controls[controlName].ForeColor = Color.Red;
                return false;
            }
            

            
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            groupBox2.Visible = false;
            groupBox2.Text = string.Empty;
            if (bloadQuestion)
                loadQuestion();
        }

        private void button5050_Click(object sender, EventArgs e)
        {
            var incorrectAnswers = new List<int>();
            foreach (CheckBox chkbox in groupBox1.Controls)
            {
                
                var  selectedQuestion = Convert.ToInt32(chkbox.Name.Substring(8));
                if (selectedQuestion != currectAnswer + 1)
                {
                    incorrectAnswers.Add(selectedQuestion);
                }
            }
            var randomidx = new Random().Next(0, 2);
            var randomincorrect = incorrectAnswers[randomidx];
            foreach (CheckBox chkbox in groupBox1.Controls)
            {
                chkbox.Visible = false;
                var selectedQuestion = Convert.ToInt32(chkbox.Name.Substring(8));
                if (selectedQuestion == currectAnswer + 1 || selectedQuestion == randomincorrect)
                {
                    chkbox.Visible = true;
                    
                }
            }
            button5050.Enabled = false;


        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            secondremaining--;

            if (secondremaining <=-1)
            {
                timer1.Enabled = false;
                SoundPlayer simpleSound = new SoundPlayer(@"c:\Windows\Media\tada.wav");
                simpleSound.Play();

            }
            else
            {
                showTimeRemaining();
            }

        }
        int secondremaining = 30;
        private void buttonPhoneAFriend_Click(object sender, EventArgs e)
        {
            groupBox2.Text = "Consulting a colleage..";
            groupBox2.Visible = true;
             secondremaining = 30;
            timer1.Enabled = true; ;
            showTimeRemaining();
            buttonPhoneAFriend.Enabled = false;
        }

        private void showTimeRemaining()
        {
            System.Windows.Forms.Application.DoEvents();
            labelResult.Text = secondremaining.ToString();
            
            showResult("second(s) remaining");
            SoundPlayer simpleSound = new SoundPlayer(@"c:\Windows\Media\Speech Off.wav");
            simpleSound.Play();

        }

        private void buttonAudience_Click(object sender, EventArgs e)
        {
            groupBox2.Text = "Consulting a the audience";
            groupBox2.Visible = true;
            secondremaining = 30;
            timer1.Enabled = true; ;
            showTimeRemaining();
            buttonAudience.Enabled = false;
        }

        private void frmMain_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '1')
            {
                button5050.Enabled = !button5050.Enabled;
            }
            if (e.KeyChar == '2')
            {
                buttonPhoneAFriend.Enabled = !buttonPhoneAFriend.Enabled;
            }
            if (e.KeyChar == '3')
            {
                buttonAudience.Enabled = !buttonAudience.Enabled;
            }
            if (e.KeyChar == '4')
            {
                timer1.Enabled = false;
            }
            if (e.KeyChar == '5')
            {
                buttonTrial.Visible = !buttonTrial.Visible;
            }
        }

        private void buttonTrial_Click(object sender, EventArgs e)
        {
            if (currentQuestion < (totalQuestions-1))
                currentQuestion++;
            loadQuestion();
        }
    }
}
